// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// U [author] rhyme_lph
// u [email]  rhymelph@gmail.com
// S [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/sub_billing_item_entity.dart';
import 'package:finance_app/src/provider/setting/subscription/sub_company_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/utils/cash_utils.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/widgets/button/square_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/list_item/sub_billing_header_item.dart';
import 'package:finance_app/src/widgets/list_item/sub_billing_item.dart';
import 'package:finance_app/src/widgets/list_item/sub_company_billing_item.dart';
import 'package:finance_app/src/utils/string_extension.dart';
import 'package:flutter/material.dart';

export 'package:finance_app/src/entities/plans_item_entity.dart';

@RouterMeta(paramName: "subCompanyPage")
class SubCompanyPage extends StatefulWidget {
  final SubBillingItemEntity subBillingItem;

  const SubCompanyPage({Key key, this.subBillingItem}) : super(key: key);

  @override
  _SubCompanyPageState createState() => _SubCompanyPageState();
}

class _SubCompanyPageState extends State<SubCompanyPage>
    with PagePresenter<SubCompanyPage, SubCompanyProvider> {
  @override
  void initState() {
    provider.subBillingItem = widget.subBillingItem;
    super.initState();
  }

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: widget.subBillingItem.companyName,
      );

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: ListView(
          padding: EdgeInsets.only(top: getDp(20)),
          children: [
            SubPaymentItem(children: [
              SpaceBetweenItem(
                  title: S.current.plan,
                  value: widget.subBillingItem.companyName != null
                      ? widget.subBillingItem.planName.toCapitalized()
                      : Config.currentCompany.name),
              SpaceBetweenItem(
                  title: S.current.valid_from,
                  value: widget.subBillingItem.billingPeriod != null
                      ? widget.subBillingItem.billingPeriod
                      : ''),
              SpaceBetweenItem(
                  title: S.current.next_bill,
                  value:
                      '\$${widget.subBillingItem.originalPrice.toStringAsFixed(2)}'),
            ]),
            AppSpaces.h20,
            SquareButton(
              title: S.current.pay_now,
              onTap: provider.onPay,
            ),
            AppSpaces.h20,
            listBuilder<int>(
              selector: (_, __) => __.pastBills.length,
              builder: (BuildContext context, int value, Widget child) =>
                  value > 0
                      ? Padding(
                          padding: const EdgeInsets.only(left: 25, right: 28),
                          child: Column(
                            children: [
                              SubBillingHeaderItem(
                                title: S.current.view_past_bills,
                              ),
                              AppSpaces.h15,
                              listBuilder<int>(
                                selector: (_, __) => __.pastBills.length,
                                builder: (BuildContext context, int value,
                                        Widget child) =>
                                    Column(
                                  children:
                                      List.generate(value, _buildPastBills),
                                ),
                              ),
                            ],
                          ),
                        )
                      : Container(),
            ),
            AppSpaces.h40,
          ],
        ),
      );

  Widget _buildPastBills(int index) {
    return listBuilder<SubBillingItemEntity>(
      selector: (_, __) => __.pastBills[index],
      builder:
          (BuildContext context, SubBillingItemEntity value, Widget child) =>
              Padding(
        padding: EdgeInsets.only(bottom: getDp(15)),
        child: SubBillingItem(
            currency: value.currencyCode,
            amount: CashUtils.formatCashAmount(value.price),
            companyName: value.companyName,
            isHistory: true,
            billDate:
                DateTimeUtils.toDateTime(value.issuedAt, 'yyyy-MM-dd HH:mm:ss'),
            onTap: () => provider.onNavBillDetail(value)),
      ),
    );
  }
}
